package com.yanchangapp.ui.fragment.purchase;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yanchangapp.R;
import com.yanchangapp.adapter.RecordListAdapter;
import com.yanchangapp.commom.RecordStatus;
import com.yanchangapp.model.bean.RecordListBean;
import com.yanchangapp.ui.activity.purchase.record.GoodsRecordActivity;
import com.yanchangapp.ui.activity.purchase.record.RecordDetailActivity;
import com.yanchangapp.ui.activity.purchase.record.RecordRejectActivity;
import com.yanchangapp.ui.presenter.GoodsRecordFgPresenter;
import com.yanchangapp.ui.view.GoodsRecordFgView;
import com.zxl.baselib.ui.base.BaseFragment;
import com.zxl.baselib.util.ui.UIUtils;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;

/**
 * Created by apple on 2019-08-23.
 * description:采购记录Fragment
 *
 * 这里showError的时机  后面进行调整
 *
 * 错误界面的问题...
 */
public class GoodsRecordFragment extends BaseFragment<GoodsRecordFgView, GoodsRecordFgPresenter> implements GoodsRecordFgView {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;
    @BindView(R.id.tvDes)
    TextView tvDes;
    @BindView(R.id.llEmptyMsg)
    LinearLayout llEmptyMsg;

    private String title;
    public static final String TITLE = "title";
    public static final String VALUE = "VALUE";
    private static final String TAG = "tab页";
    private boolean isShow = false;
    private boolean isLoading = false;
    private RecordListAdapter mRecordListAdapter;
    private List<RecordListBean> mRecordListBeans = new ArrayList<>();
    // 请求体需要的各个参数
    private String paramsValue;
    private int curPage = 1;

    // 传递树值
    @Override
    public void init() {
        if (getArguments() != null) {
            title = getArguments().getString(TITLE);
            // 赋予值....
            paramsValue = getArguments().getString(VALUE);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isShow = isVisibleToUser;
        if (isVisibleToUser) {
            if (!TextUtils.isEmpty(title)) {
                isLoading = true;
                loadList();
            } else {
                isLoading = false;
            }
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_goods_record_list;
    }

    @Override
    protected GoodsRecordFgPresenter createPresenter() {
        return new GoodsRecordFgPresenter((GoodsRecordActivity) getActivity());
    }

    @Override
    public void initView(View rootView) {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecordListAdapter = new RecordListAdapter(R.layout.item_record_list, mRecordListBeans);
        recyclerView.setAdapter(mRecordListAdapter);
        if (isShow && !isLoading) {
            loadList();
        }
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            // 直接刷新列表即可
            refreshList(true);
        });
        tvDes.setText("暂无采购记录");
        refreshLayout.setOnLoadMoreListener(refreshLayout -> loadMore());
        // 设置item条目的点击事件...
        mRecordListAdapter.setOnItemClickListener((adapter, view, position) -> {
            // 携带ID进行页面跳转...
            int recordID = mRecordListBeans.get(position).getId();
            int status = mRecordListBeans.get(position).getStatus();
            if (RecordStatus.HAS_REJECT.getStatus().equals(String.valueOf(status))){
                // todo 跳转向已驳回的界面信息
                RecordRejectActivity.show(getActivity(),recordID);
            }else {
                RecordDetailActivity.show(getActivity(),recordID,true);
            }
        });

        // 刷新点击事件,后面去处理
        btnNextRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadList();
            }
        });
    }

    // 加载更多
    private void loadMore() {
        curPage++;
        // 上拉加载更多时,不是拉动
        mPresenter.queryRecordsList(paramsValue,curPage,false);
    }

    private void loadList() {
        // 滑动到了第一个....
        recyclerView.scrollToPosition(0);
        refreshList(false);
    }

    // 刷新列表 ==> 是否下拉
    private void refreshList(boolean isPulling) {
        curPage = 1;
        refreshLayout.resetNoMoreData();
        mPresenter.queryRecordsList(paramsValue,curPage,isPulling);
    }

    // 设置各种显示的状态
    private void showContent(){
        if (llErrorNetwork.getVisibility() == View.VISIBLE){
            llErrorNetwork.setVisibility(View.GONE);
        }

        if (refreshLayout.getVisibility() == View.GONE){
            refreshLayout.setVisibility(View.VISIBLE);
        }

        if (llEmptyMsg.getVisibility() == View.VISIBLE){
            llEmptyMsg.setVisibility(View.GONE);
        }

    }

    private void showEmpty(){
        if (llErrorNetwork.getVisibility() == View.VISIBLE){
            llErrorNetwork.setVisibility(View.GONE);
        }

        if (refreshLayout.getVisibility() == View.VISIBLE){
            refreshLayout.setVisibility(View.GONE);
        }

        if (llEmptyMsg.getVisibility() == View.GONE){
            llEmptyMsg.setVisibility(View.VISIBLE);
        }
    }

    private void showError(){
        if (llErrorNetwork.getVisibility() == View.GONE){
            llErrorNetwork.setVisibility(View.VISIBLE);
        }

        if (refreshLayout.getVisibility() == View.VISIBLE){
            refreshLayout.setVisibility(View.GONE);
        }

        if (llEmptyMsg.getVisibility() == View.VISIBLE){
            llEmptyMsg.setVisibility(View.GONE);
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {

    }

    // 提供父亲让其刷新,这里不需要接收参数,只需要让其刷新即可...
    public void handleRefresh() {
        loadList();
    }


    @Override
    public void queryRecordListSuccess(List<RecordListBean> mRecordsList) {
        if (curPage == 1) {
            mRecordListBeans.clear();
            refreshLayout.finishRefresh();
            if (mRecordsList == null || mRecordsList.size() == 0) {
                // 空布局
                showEmpty();
            } else {
                // 含有内容
                showContent();
                mRecordListBeans.addAll(mRecordsList);
            }
            mRecordListAdapter.notifyDataSetChanged();
        } else {
            if (mRecordsList == null || mRecordsList.size() == 0) {
                // 下拉加载无更多内容
                refreshLayout.finishLoadMoreWithNoMoreData();
                curPage--;
            } else {
                // 含有内容
                mRecordListBeans.addAll(mRecordsList);
                mRecordListAdapter.notifyDataSetChanged();
                refreshLayout.finishLoadMore();
            }
        }
    }

    @Override
    public void queryRecordListFailure(String err) {
        UIUtils.showToast(err);
        showError();
        if (curPage == 1) {
            refreshLayout.finishRefresh();
        } else {
            curPage--;
            refreshLayout.finishLoadMore();
        }
    }
}
